package ocean.chinacraft.common.world.block.function.container;

import ocean.chinacraft.common.world.block.function.FurnacePlatformBlock;
import ocean.chinacraft.common.ModContainer;
import ocean.chinacraft.common.ModBlocks;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IWorldPosCallable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;
import net.minecraftforge.items.wrapper.InvWrapper;

import javax.annotation.Nonnull;

/**
 * 灶台容器
 *
 * @author zhang
 * @since 2021/11/15
 */
public class FurnacePlatformBlockContainer extends Container {

    private final TileEntity tileEntity;

    private final PlayerEntity playerEntity;

    private final IItemHandler playerInventory;

    public FurnacePlatformBlockContainer(int containerId, World world, BlockPos pos, PlayerInventory playerInventory, PlayerEntity player) {
        super(ModContainer.FURNACE_PLATFORM_BLOCK_CONTAINER.get(), containerId);
        this.tileEntity = world.getBlockEntity(pos);
        this.playerEntity = player;
        this.playerInventory = new InvWrapper(playerInventory);
        layoutPlayerInventorySlots(9, 86);
        if (tileEntity != null) {
            tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(h -> {
                addSlot(new SlotItemHandler(h, 0, 81, 33));
                addSlot(new SlotItemHandler(h, 1, 81, 55));
            });
        }
    }

    @Override
    public boolean stillValid(@Nonnull PlayerEntity player) {
        return Container.stillValid(IWorldPosCallable.create(tileEntity.getLevel(), tileEntity.getBlockPos()),
                player, ModBlocks.FURNACE_PLATFORM_BLOCK.get());
    }

    @Nonnull
    @Override
    public ItemStack quickMoveStack(@Nonnull PlayerEntity player, int index) {
        //TODO 可能添加shift快速移动功能
        return ItemStack.EMPTY;
    }

    private int addSlotRange(IItemHandler handler, int index, int x, int y, int amount, int dx) {
        for (int i = 0; i < amount; i++) {
            addSlot(new SlotItemHandler(handler, index, x, y));
            x += dx;
            index++;
        }
        return index;
    }

    private int addSlotBox(IItemHandler handler, int index, int x, int y, int horAmount, int dx, int verAmount, int dy) {
        for (int j = 0; j < verAmount; j++) {
            index = addSlotRange(handler, index, x, y, horAmount, dx);
            y += dy;
        }

        return index;
    }

    private void layoutPlayerInventorySlots(int leftCol, int topRow) {
        addSlotBox(playerInventory, 9, leftCol, topRow, 9, 18, 3, 18);
        topRow += 58;
        addSlotRange(playerInventory, 0, leftCol, topRow, 9, 18);
    }

    public boolean isFire() {
        return tileEntity.getBlockState().getValue(FurnacePlatformBlock.FIRE);
    }
}
